const {ListNode, getListNodeFromArray, getArrayFromListNode, logList, reverseLn} = require('../../utils/list.js')

/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode} 
 */
const addTwoNumbers2 = function (l1, l2) {
  let stacks1 = [], stacks2 = [];
  let cur = null
  let pre = null
  let carry = 0
  while (l1) {
    stacks1.push(l1.val)
    l1 = l1.next;
  }
  while (l2) {
    stacks2.push(l2.val)
    l2 = l2.next;
  }
  while (stacks1.length > 0 || stacks2.length > 0 || carry > 0) {
    let n1 = stacks1.length > 0 ? stacks1.pop() : 0
    let n2 = stacks2.length > 0 ? stacks2.pop() : 0
    let sum = n1 + n2 + carry
    cur = new ListNode(sum % 10)
    cur.next = pre
    pre = cur
    carry = Math.floor(sum / 10)
  }
  return cur
}

logList(addTwoNumbers2(getListNodeFromArray([7,2,4,3]), getListNodeFromArray([5,6,4])))
